//
//  ZBCarAnimationView.m
//  送礼
//
//  Created by 华惠友 on 2017/9/19.
//  Copyright © 2017年 华惠友. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ZBCarAnimationView.h"

@interface ZBCarAnimationView ()
@property (nonatomic, copy) AnimationFinishBlock finishedBlock;

@end

@implementation ZBCarAnimationView
static ZBCarAnimationView *_animationView = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _animationView = [[self alloc] init];
    });
    return _animationView;
}

//- (instancetype)init {
//    self = [super init];
//    if (self) {
//
//        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//    }
//    return self;
//}

- (void)animationWithCar:(AnimationFinishBlock)finished {
    self.isAnimating = YES;     // 正在执行动画
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    self.finishedBlock = finished;
    
    CGFloat durTime = 3.0;
    
    UIImageView *porsche918 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"porsche"]];
    
    //设置汽车初始位置
    porsche918.frame = CGRectMake(0, 0, 0, 0);
    [self addSubview:porsche918];
    
    //给汽车添加动画
    [UIView animateWithDuration:durTime animations:^{
        
        porsche918.frame = CGRectMake(SCREEN_WIDTH * 0.5 - 100, SCREEN_HEIGHT * 0.5 - 100 * 0.5, 240, 120);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(durTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //
        [UIView animateWithDuration:0.5 animations:^{
            porsche918.alpha = 0;
        } completion:^(BOOL finished) {
            self.isAnimating = NO;     // 动画结束
            [self removeFromSuperview];
            self.finishedBlock();
        }];
    });

}
@end
